﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    public class SysParam
    {
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FFParamNo,FEntityNo,FFParamName,FFValue,FFNote,FFconfirm,FFCanModify,FFContent ");
            strSql.Append(" FROM t_SysParam ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.SysParam model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_SysParam(");
            strSql.Append("FFParamNo,FEntityNo,FFParamName,FFValue,FFNote,FFconfirm,FFCanModify,FFContent)");
            strSql.Append(" values (");
            strSql.Append("@FFParamNo,@FEntityNo,@FFParamName,@FFValue,@FFNote,@FFconfirm,@FFCanModify,@FFContent)");
            SqlParameter[] parameters = {
					new SqlParameter("@FFParamNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FFParamName", SqlDbType.NVarChar,50),
					new SqlParameter("@FFValue", SqlDbType.NVarChar,100),
					new SqlParameter("@FFNote", SqlDbType.NVarChar,255),
					new SqlParameter("@FFconfirm", SqlDbType.NChar,1),
					new SqlParameter("@FFCanModify", SqlDbType.NChar,1),
					new SqlParameter("@FFContent", SqlDbType.NText)};
            parameters[0].Value = model.FFParamNo;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FFParamName;
            parameters[3].Value = model.FFValue;
            parameters[4].Value = model.FFNote;
            parameters[5].Value = model.FFconfirm;
            parameters[6].Value = model.FFCanModify;
            parameters[7].Value = model.FFContent;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FFParamNo, string FEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_SysParam");
            strSql.Append(" where FFParamNo=@FFParamNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFParamNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = FFParamNo;
            parameters[1].Value = FEntityNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 保存系统参数
        /// <cretor>wangl</cretor>
        /// <date>2012-12-06</date>
        /// </summary>
        /// <param name="sysList"></param>
        public void SaveSysparam(List<Model.SysParam> sysList)
        {
            foreach (Model.SysParam sys in sysList)
            {
                //判断是否存在，存在就修改，不存在就添加
                if (Exists(sys.FFParamNo, sys.FEntityNo))
                {
                    //修改
                    UpdateValue(sys);
                }
                else
                {
                    //增加
                    Add(sys);
                }
            }
        }

        /// <summary>
        /// 更新参数
        /// <cretor>wangl</cretor>
        /// <date>2012-12-06</date>
        /// </summary>
        public void UpdateValue(Model.SysParam model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_SysParam set ");
            strSql.Append("FFValue=@FFValue");
            strSql.Append(" where FFParamNo=@FFParamNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFParamNo", SqlDbType.VarChar,20),
					new SqlParameter("@FFValue", SqlDbType.NVarChar,100),					
					new SqlParameter("@FEntityNo", SqlDbType.VarChar,12)};
            parameters[0].Value = model.FFParamNo;
            parameters[1].Value = model.FFValue;
            parameters[2].Value = model.FEntityNo;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 获取参数值
        /// <cretor>wangl</cretor>
        /// <date>2013-08-05</date>
        /// </summary>
        /// <param name="paramNo">参数编号</param>
        /// <param name="entityNo">公司编号</param>
        /// <returns></returns>
        public string GetParamValue(string paramNo, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FFValue from t_SysParam");
            strSql.Append(" where FFParamNo=@FFParamNo and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FFParamNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = paramNo;
            parameters[1].Value = entityNo;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (null != obj)
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }


        }
    }
}
